package persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import modelo.Anuncio;


public class DAOAnuncio {
	
	 private Connection conexao;
	 
	  private void obterConexao(){
	       try {
	           Class.forName("com.mysql.jdbc.Driver");
	           conexao = DriverManager.getConnection("jdbc:mysql://localhost/sirp", "root", "123");
	       } catch (ClassNotFoundException e) {
	           e.printStackTrace();
	       } catch (SQLException e) {
	           e.printStackTrace();
	       }
	   }
	   
	  private void liberarConexao(){
	        try {
	            conexao.close();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }    
	    	public void inserir(Anuncio anuncio){
	        obterConexao();
	        try {
	            PreparedStatement pstat = conexao.prepareStatement("INSERT INTO Anuncio (id) VALUES (?)");
	            pstat.setInt(1, anuncio.getId());
	            pstat.executeUpdate();
	            
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        liberarConexao();
	    }
	    
	    	public void atualizar(Anuncio anuncio){
	        obterConexao();
	        try {
	            PreparedStatement pstat = conexao.prepareStatement("UPDATE Anuncio SET id = ? WHERE id = ? ");
	            pstat.setInt(1, anuncio.getId());
	            pstat.executeUpdate();
	            
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        liberarConexao();
	    }

}
